/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "flash.h"
#include "pwrmgr.h"
#include "hal_hall.h"
#include "hal_motor.h"
#include "hal_button.h"
#include "hal_battery.h"
#include "app_misc.h"
#include "app_reset.h"
#include "app_motor.h"
#include "app_pwr_mgr.h"
#include "app_battery.h"
#include "app_display.h"
#include "app_store_mem.h"
#include "app_global_data.h"
#include "app_store_set_info.h"
#include "bsp_hw_config.h"
#include "user_config.h"


/*----------------------------macro file---------------------------*/
#define hal_store_erase_sector  hal_flash_erase_sector
#define hal_store_write         hal_flash_write
#define hal_store_read          hal_flash_read

#define WDT_FEED()

#if(WORK_IN_SLEEP_MODE)
#define SYS_SLEEP_ENTER()       app_pwr_mgr_enter_sleep_directly()
#else
#define SYS_SLEEP_ENTER()       app_pwr_mgr_enter_sleep()
#endif

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

void app_peripheral_init(void)
{
    app_system_reset_data_init();
    app_mem_data_init();
    app_set_info_init();
    app_batt_hw_init();
    app_motor_init();
    g_app_data.sys.state.btn_lock = true;
}


void app_ship_mode_enter(void)
{
    uint16_t reset_reason = SHIP_MODE_WAKE_ID;
    hal_flash_write(TEMP_DATA_RST_ADDR, (uint8_t*)&reset_reason, sizeof(reset_reason));

    while(true == hal_button_press_state_get()) {	//Wait button release.
        WDT_FEED();
        app_delay_ms(50);
    }
    SYS_SLEEP_ENTER();
    //////uart_int_disable();
    hal_hall_hw_deinit();
    app_disp_deinit();
    hal_motor_stop();
    hal_batt_adc_deinit();
    hal_batt_charge_hw_deinit();
#if(BOOST_POWER_CTRL_EN)
    app_peripheral_boost_power_disable();
#endif
    g_app_data.sys.mode = DEV_MODE_DEEP_SLEEP;
    pwroff_cfg_t pwr_wkp_cfg = {BSP_BUTTON_PIN_NUM, NEGEDGE}; //Button wake up.
	hal_pwrmgr_poweroff(&pwr_wkp_cfg, 1);
	while(1);
}

#if(BOOST_POWER_CTRL_EN)

#define BOOST_OUTPUT_INIT()     hal_gpio_pin_init(BSP_BOOST_PWR_PIN_NUM, GPIO_OUTPUT)
#define BOOST_OUTPUT_LOW()      hal_gpio_write(BSP_BOOST_PWR_PIN_NUM, 0)
#define BOOST_OUTPUT_HIGH()     hal_gpio_write(BSP_BOOST_PWR_PIN_NUM, 1)

void app_peripheral_boost_power_enable(void)
{
#if(WORK_IN_SLEEP_MODE)
    hal_gpioretention_register(BSP_BOOST_PWR_PIN_NUM);
#endif  //#if(WORK_IN_SLEEP_MODE)
    BOOST_OUTPUT_INIT();
    BOOST_OUTPUT_HIGH();
    app_delay_ms(50);
}

void app_peripheral_boost_power_disable(void)
{
#if(WORK_IN_SLEEP_MODE)
    hal_gpioretention_register(BSP_BOOST_PWR_PIN_NUM);
#endif  //#if(WORK_IN_SLEEP_MODE)
    BOOST_OUTPUT_INIT();
    BOOST_OUTPUT_LOW();
}
#endif
